package com.cskaoyan.mall.controller.promotion;

import com.cskaoyan.mall.bean.Ad;
import com.cskaoyan.mall.bean.vo.ResultVO;
import com.cskaoyan.mall.service.AdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/ad")
public class AdController {
    @Autowired
    AdService adService;
    @RequestMapping("list")
    public ResultVO getAdList(Integer page,Integer limit,String sort,String order,String name,String content){
        List<Ad> list = adService.getAdList(page,limit,sort,order,name,content);
        Map<String, Object> map = new HashMap<>();
        map.put("total",list.size());
        map.put("items",list);
        return ResultVO.ok(map);
    }

    @PostMapping("delete")
    public ResultVO deleteAd(@RequestBody Ad ad){
        adService.deleteAdById(ad.getId());
        return ResultVO.ok();
    }

    @PostMapping("update")
    public ResultVO updateAd(@RequestBody Ad ad){
        Ad adVo = adService.updateAd(ad);
        return ResultVO.ok(adVo);
    }

    @PostMapping("create")
    public ResultVO create(@RequestBody Ad ad){
        Ad adVo = adService.createAd(ad);
        return ResultVO.ok(adVo);
    }

}
